﻿

namespace Mulala.Cavan.Application.MainBoundedContext.MulalaModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Domain.MainBoundedContext.MulalaModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SecurityModule;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;
    using Mulala.Cavan.Domain.MainBoundedContext.ProprietorModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Application.MainBoundedContext.SecurityModule;
    using Mulala.Cavan.Domain.MainBoundedContext.DictionaryModule;
    using Mulala.Cavan.Domain.MainBoundedContext.CorporationModule;
    using Mulala.Cavan.Domain.MainBoundedContext.NannyModule;
    using Spring.Context;
    using Spring.Context.Support;

    public class MulalaRegistrationAppService : IMulalaRegistrationAppService
    {
        private IRoleAppService RoleAppService { get; set; }
        private IAreaAppService AreaAppService { get; set; }
        private IProprietorRepository ProprietorRepository { get; set; }
        private ICorporationRepository CorporationRepository { get; set; }
        private INannyRepository NannyRepository { get; set; }
        private IApplicationContext springContext { get; set; }

        public ProprietorRegistrationResult AddProprietor(
                                                string name, 
                                                string password, 
                                                string email,
                                                string areaCode)
        {
            springContext = ContextRegistry.GetContext();
            this.AreaAppService = springContext.GetObject(MulalaAppServiceConst.AREAAPPSERVICE) as IAreaAppService;

            IList<Role> roles = GetRoles(SecurityIdentifier.Role.Proprietor.ToString());

            Area area = this.AreaAppService.GetById(areaCode);

            IRegistrationService registrationService = new RegistrationService();

            ProprietorRegistrationResult result = registrationService.RegisterProprietor(name, password, email, area, roles);

            this.ProprietorRepository.Save(result.Object);

            return result;
        }

        public CorporationRegistrationResult AddCorporation(
                                                string name, 
                                                string password, 
                                                string email,
                                                string areaCode)
        {
            springContext = ContextRegistry.GetContext();
            this.AreaAppService = springContext.GetObject(MulalaAppServiceConst.AREAAPPSERVICE) as IAreaAppService;

            IList<Role> roles = GetRoles(SecurityIdentifier.Role.Corporation.ToString());
            Area area = this.AreaAppService.GetById(areaCode);
            IRegistrationService registrationService = new RegistrationService();
            CorporationRegistrationResult result = registrationService.RegisterCorporation(name, password, email, area, roles);

            this.CorporationRepository.Save(result.Object);
            
            return result;
        }

        public NannyRegistrationResult AddNanny(
                                            string name, 
                                            string password, 
                                            string email,
                                            string areaCode)
        {
            springContext = ContextRegistry.GetContext();
            this.AreaAppService = springContext.GetObject(MulalaAppServiceConst.AREAAPPSERVICE) as IAreaAppService;

            IList<Role> roles = GetRoles(SecurityIdentifier.Role.Nanny.ToString());
            Area area = this.AreaAppService.GetById(areaCode);
            Corporation corporation = this.CorporationRepository.Get(27);

            IRegistrationService registrationService = new RegistrationService();
            NannyRegistrationResult result = registrationService.RegisterNanny(name, password, email, area, roles, corporation);
            this.NannyRepository.Save(result.Object);

            return result;
        }

        private IList<Role> GetRoles(string roleName)
        {
            Role role = this.RoleAppService.GetRoleByName(roleName);
            IList<Role> roles = new List<Role>();
            roles.Add(role);

            return roles;
        }
    }
}
